package com.huiztech.maystall.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.huiztech.maystall.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

public class WaterfallAdapter extends BaseAdapter {

	ArrayList<String> list;
	Context context;
	private Drawable drawable;

	class Holder {
		public ImageView imageView;
		public TextView contentTextView;
		public TextView priceTextView;
		public TextView likeTextView;
		public ProgressBar loadingBar;
	}

	public WaterfallAdapter(ArrayList<String> list, Context context) {
		this.list = list;
		this.context = context;
	}

	@Override
	public int getCount() {
		return list != null ? list.size() : 0;
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(final int position, View view, ViewGroup group) {
		final Holder holder;
		// 得到View
		if (view == null) {
			holder = new Holder();
			LayoutInflater inflater = LayoutInflater.from(context);
			view = inflater.inflate(R.layout.main_list_item, null);
			holder.imageView = (ImageView) view
					.findViewById(R.id.main_item_image);
			holder.priceTextView = (TextView) view
					.findViewById(R.id.main_item_price_text);
			holder.likeTextView = (TextView) view
					.findViewById(R.id.main_item_like_text);
			holder.contentTextView = (TextView) view
					.findViewById(R.id.main_item_content_text);
			holder.loadingBar = (ProgressBar) view
					.findViewById(R.id.main_item_load);

			view.setTag(holder);
		} else {
			holder = (Holder) view.getTag();
		}

		holder.priceTextView.setText("价钱");
		holder.likeTextView.setText("喜欢人数");
		holder.contentTextView.setText("这是一个好东东，真的是一个好东东！");

		ImageLoader.getInstance().displayImage(list.get(position),
				holder.imageView, new ImageLoadingListener() {

					@Override
					public void onLoadingStarted(String arg0, View arg1) {
						holder.loadingBar.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						String message = null;
						switch (failReason.getType()) {
						case IO_ERROR:
							message = "Input/Output error";
							break;
						case DECODING_ERROR:
							message = "can not be decoding";
							break;
						case NETWORK_DENIED:
							message = "Downloads are denied";
							break;
						case OUT_OF_MEMORY:
							message = "内存不足";
							Toast.makeText(context, message, Toast.LENGTH_SHORT)
									.show();
							break;
						case UNKNOWN:
							message = "Unknown error";
							Toast.makeText(context, message, Toast.LENGTH_SHORT)
									.show();
							break;
						}
						holder.loadingBar.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingComplete(String arg0, View arg1,
							Bitmap arg2) {
						holder.loadingBar.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingCancelled(String arg0, View arg1) {

					}
				});

		return view;
	}

}
